package cn.uyunsky.platform.exception;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.util.Locale;

import cn.uyunsky.platform.BeanFactoryUtil;
import cn.uyunsky.platform.util.holder.MessageSourceHolder;

public class PLException extends RuntimeException {

	private static final long serialVersionUID = 1L;

	//异常代码  
	private String key;

	private Object[] values;

	public PLException(String key) {
		new PLException(key, null, null);
	}

	public PLException(String key, Throwable throwable) {
		new PLException(key, null, null);
	}

	public PLException(String key, Object value) {
		new PLException(key, new Object[] { value }, null);
	}

	public PLException(String key, Object value, Throwable throwable) {
		new PLException(key, new Object[] { value }, throwable);
	}

	public PLException(String key, Object[] values, Throwable throwable) {
		super(throwable);
		this.key = key;
		this.values = values;
	}

	public String getKey() {
		return key;
	}

	public Object[] getValues() {
		return values;
	}

	@Override
	public String getMessage() {
		return MessageSourceHolder.getMessage(key, values, Locale.getDefault());
	}

	public String getStackMessage() {
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		PrintStream ps = new PrintStream(baos);
		this.printStackTrace(ps);
		return baos.toString();
	}

	public static void main(String[] args) {
		BeanFactoryUtil.getBean("messageSource");
		String key = "message";
		Object value = "哈哈";
		PLException e = new PLException(key, value);
		System.out.println(e.getMessage());
		System.out.println(e.toString());
		System.out.println(e.getStackMessage());
	}

}
